"""x=int(input("x=?"))
y=2
z=0

while x>1:
    if z==0:
        if x%y==0:
            print(y)
            print(int(x/y))
            x=int(x/y)
            z=x/y
    while y<x+1 and z>0:#3
        y=3
        if x%y==0:
            print(y)
            print(int(x/y))            
            z=x/y
            x=int(x/y)
            y+=x*x
        y+=2
    y=2
"""
"""
x=int(input("x=?"))
y=2
while x>1:
    while y<x+1:
        if x%y==0:
            print(y)          
            x=int(x/y)
            y+=x*x
        if y==2:
            y-=1
        y+=2
    y=2
"""

x=int(input("x=?"))
y=2
z=1
a=y
while x>1 and z==1: 
    z=0
    while y*y<x+1:#3 使用y进行尝试，其中y只需要尝试到 x的平方根就可以
        if x%y==0: # 符合要求
            print(y) # 打印x的一个分解项
            x=int(x/y)
            a=y
            y+=x*x # 向前跳跃，加快遍历，但是为什么是x*x?
            # 如果x,y是原值x的分解,那么下一个可能的分解项
            z=1 # 标记已经找到1个分解项
        if y==2:
            y-=1
        y+=2 #默认的前进步幅，至少的前进步数
    y=a
print(x)
import pygame
import os

ROW=3
COLUMN=4
B=200
W=B*COLUMN
H=B*(ROW+1)

EXT=None
EXT_X=B*3
EXT_Y=B*3

def splitImage(bg_img:pygame.Surface):
    w=bg_img.get_width()
    h=bg_img.get_height()
    imgs=[]
    for j in range(ROW):
        sub_imgs=[]
        for i in range(COLUMN):
            sm_img=pygame.Surface((B,B))
            sm_img.blit(bg_img,(-(i*B),-(j*B)))
            sub_imgs.append(sm_img)
        imgs.append(sub_imgs)
    return imgs

def getBlock(bs,x,y):
    return bs[y][x]

def drawAllBlock(screen,bs):
    for j in range(ROW):
        for i in range(COLUMN):
            b=getBlock(bs,i,j)
            screen.blit(b,(i*B,j*B))

# 移动图片
# 并返回是否移动成功
def moveBlock():
    pass

def main():
    pygame.init()
    pygame.display.set_caption("拼图游戏")
    img_wh=img_w,img_h=W,B*ROW
    sc_wh=screen_w,screen_h=W,H
    screen=pygame.display.set_mode((screen_w,screen_h))

    clock=pygame.time.Clock()

    bg_img=pygame.image.load("./bg01.jpg").convert()
    bg_img=pygame.transform.scale(bg_img,img_wh)
    bg_img.get_width

    screen.fill((255,255,255))
    bs=splitImage(bg_img)

    run=True
    while run:
        clock.tick(60)
        # screen.blit(bg_img,(0,0))
        drawAllBlock(screen,bs)

        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                run=False
            elif event.type == pygame.MOUSEBUTTONUP:
                print(event.button)
                print(event.pos)
                if event.button==1:
                    x,y=event.pos
                    i=x//B
                    j=y//B
                    b=getBlock(bs,i,j)
                    if b:
                        moveBlock(b,i,j)
                    else:
                        print("can not fong block")
            
        pygame.display.update()
    pygame.quit()

main()

"""
pip install Pillow  -- 安装 Pillow

curl "https://tenfei03.cfp.cn/creative/vcg/veer/800/new/VCG41N644372310.jpg" -o v2.jpg
curl "" -o v2.jpg

"""
import pygame
import os
import random
import time
can_move
ROW=3
COLUMN=4
B=200
W=B*COLUMN
H=B*(ROW+1)

EXT=None
EXT_X=B*3
EXT_Y=B*3

random.seed(time.time_ns())

def splitImage(bg_img:pygame.Surface):
    w=bg_img.get_width()
    h=bg_img.get_height()
    imgs=[]
    for j in range(ROW):
        sub_imgs=[]
        for i in range(COLUMN):
            sm_img=pygame.Surface((B,B))
            sm_img.blit(bg_img,(-(i*B),-(j*B)))
            sub_imgs.append(sm_img)
        imgs.append(sub_imgs)
    return imgs

def getBlock(bs,x,y):
    if x>=0 and x<COLUMN and y>=0 and y<ROW:
        return bs[y][x]
    return None

def drawAllBlock(screen,bs):
    pygame.draw.rect(screen,[255,255,255],[0,0,W,H],0)
    for j in range(ROW):
        for i in range(COLUMN):
            b=getBlock(bs,i,j)
            if b:
                screen.blit(b,(i*B,j*B))
    if EXT:
        screen.blit(EXT,(EXT_X,EXT_Y))
    else:
        pygame.draw.rect(screen,[255,255,255],[EXT_X,EXT_Y,B,B],0)

def canMoveBlock(bs,x,y):
    global EXT
    b=getBlock(bs,x,y)
    if not b:
        if x==3 and y==3 and EXT!=None:
            if y-1>=0 and getBlock(bs,x,y-1)==None:
                #可以移动 向上
                return True
        return False
    if EXT==None and x+1==COLUMN and y+1==ROW:
        #可以移动 右下方向下到扩展块
        return True
    else:
        if y-1>=0 and getBlock(bs,x,y-1)==None:
            #可以移动 向上
            return True
        elif x+1<COLUMN and getBlock(bs,x+1,y)==None:
            #可以移动 向右
            return True
        elif y+1<ROW and getBlock(bs,x,y+1)==None:
            #可以移动 向下
            return True
        elif x-1>=0 and getBlock(bs,x-1,y)==None:
            #可以移动 向左
            return True
    return False
# 移动图片
# bs为全部小图片，x,y为要移动的块的坐标，移动方向自动判断
# 并返回是否移动成功
def moveBlock(bs,x,y):
    print(f"move {x} {y}")
    global EXT
    b=getBlock(bs,x,y)
    if not b:
        if x==3 and y==3 and EXT!=None:
            if y-1>=0 and getBlock(bs,x,y-1)==None:
                #可以移动 向上
                print(f"move to up")
                bs[y-1][x]=EXT
                EXT=None
                return True
        return False
    if EXT==None and x+1==COLUMN and y+1==ROW:
        #可以移动 右下方向下到扩展块
        print(f"move to ext")
        EXT=b
        bs[y][x]=None
        return True
    else:
        if y-1>=0 and getBlock(bs,x,y-1)==None:
            #可以移动 向上
            print(f"move to up")
            bs[y-1][x]=b
            bs[y][x]=None
            return True
        elif x+1<COLUMN and getBlock(bs,x+1,y)==None:
            #可以移动 向右
            print(f"move to right")
            bs[y][x+1]=b
            bs[y][x]=None
            return True
        elif y+1<ROW and getBlock(bs,x,y+1)==None:
            #可以移动 向下
            print(f"move to down")
            bs[y+1][x]=b
            bs[y][x]=None
            return True
        elif x-1>=0 and getBlock(bs,x-1,y)==None:
            #可以移动 向左
            print(f"move to left")
            bs[y][x-1]=b
            bs[y][x]=None
            return True
    return False

# 打乱
def shuff(bs):
    x,y=3,3 # 这里记录的是空格位置
    i=0
    while i<100:
        i+=1
        can_move=[]
        if canMoveBlock(bs,x,y-1):
            can_move.append("上")
        if canMoveBlock(bs,x,y+1):
            can_move.append("下")
        if canMoveBlock(bs,x-1,y):
            can_move.append("左")
        if canMoveBlock(bs,x+1,y):
            can_move.append("右")
        r=random.randint(0,len(can_move))
        r=can_move[r]
        print(f"{i} curr x={x}, y={y} can move={len(can_move)} r={r}")
        if r=="上":
            if moveBlock(bs,x,y-1):
                y-=1
        elif r=="右":
            if moveBlock(bs,x+1,y):
                x+=1
        elif r=="下":
            if moveBlock(bs,x,y+1):
                y+=1
        elif r=="左":
            if moveBlock(bs,x-1,y):
                x-=1


def main():
    pygame.init()
    pygame.display.set_caption("拼图游戏")
    img_wh=img_w,img_h=W,B*ROW
    sc_wh=screen_w,screen_h=W,H
    screen=pygame.display.set_mode((screen_w,screen_h))

    clock=pygame.time.Clock()

    bg_img=pygame.image.load("./v3.jpg").convert()
    bg_img=pygame.transform.scale(bg_img,img_wh)
    bg_img.get_width

    screen.fill((255,255,255))
    bs=splitImage(bg_img)
    shuff(bs)

    run=True
    while run:
        clock.tick(20)
        # screen.blit(bg_img,(0,0))
        drawAllBlock(screen,bs)

        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                run=False
            elif event.type == pygame.MOUSEBUTTONUP:
                # print(event.button)
                print(event.pos)
                if event.button==1:
                    x,y=event.pos
                    i=x//B
                    j=y//B
                    if moveBlock(bs,i,j):
                        drawAllBlock(screen,bs)
            
        pygame.display.update()
    pygame.quit()

main()

"""
pip install Pillow  -- 安装 Pillow

curl "https://tenfei03.cfp.cn/creative/vcg/veer/800/new/VCG41N644372310.jpg" -o v2.jpg
curl "" -o v2.jpg

"""http://m.biancheng.net/pygame/display.html